/**
 * 初始化参数配置表详情对话框
 */
var ConfigInfoDlg = {
    configZtree : null,
    validateFields: {
    	name: {
            validators: {
                notEmpty: {
                    message: '参数名称不能为空'
                }
            }
        },
    	paramKey: {
            validators: {
                notEmpty: {
                    message: '参数键名不能为空'
                }
            }
        },
    	paramValue: {
            validators: {
                notEmpty: {
                    message: '参数键值不能为空'
                }
            }
        },
        categoryName: {
            validators: {
                notEmpty: {
                    message: '参数分类不能为空'
                }
            }
        },
    }
};


/**
 * 关闭此对话框
 */
ConfigInfoDlg.close = function() {
    parent.layer.close(parent.layer.getFrameIndex(window.name)); //先得到当前iframe层的索引,再执行关闭   
}

/**
 * 点击父级菜单input框时
 *
 * @param e
 * @param treeId
 * @param treeNode
 * @returns
 */
ConfigInfoDlg.onClickPName = function(e, treeId, treeNode) {
    if($("#id").val()==treeNode.id&&treeNode.id!=0){
        Sys.error("父节点不要选自己啦！")
        return;
    }
    $("#categoryName").attr("value", ConfigInfoDlg.configZtree.getSelectedVal());
    $("#categoryId").attr("value", treeNode.id);
    ConfigInfoDlg.validate();
}


/**
 * 显示父级菜单的树
 *
 * @returns
 */
ConfigInfoDlg.showConfigSelectTree = function() {
    Sys.showInputTree("categoryId", "pTreeDiv", 15, 28);
}

/**
 * 验证数据是否为空
 */
ConfigInfoDlg.validate = function () {
    $('#configInfoForm').data("bootstrapValidator").resetForm();
    $('#configInfoForm').bootstrapValidator('validate');
    return $("#configInfoForm").data('bootstrapValidator').isValid();
}

/**
 * 提交新增
 */
ConfigInfoDlg.addSubmit = function() {

    if (!this.validate()) {
        return;
    }
    //提交信息
    Sys.ajaxForm('configInfoForm',Sys.backendPath + "/config/add",function(res) {
        Sys.success(res.message);
    	window.parent.Config.table.refresh();
        ConfigInfoDlg.close();
    });
}

/**
 * 提交修改
 */
ConfigInfoDlg.editSubmit = function() {

    if (!this.validate()) {
        return;
    }
    //提交信息
    Sys.ajaxForm('configInfoForm',Sys.backendPath + "/config/update",function(res) {
        Sys.success(res.message);
    	window.parent.Config.table.refresh();
        ConfigInfoDlg.close();
    });
}

$(function() {
    var _configZtree = new $ZTree("pTree", Sys.backendPath + "/config/category/tree");
    _configZtree.bindOnClick(ConfigInfoDlg.onClickPName);
    _configZtree.init();
    ConfigInfoDlg.configZtree = _configZtree;
	Sys.initValidator("configInfoForm", ConfigInfoDlg.validateFields);
});
